{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example shows how to run a script in a headless browser using AgentQL with the [Google Colaboratory](https://colab.research.google.com/) environment."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Install [AgentQL](https://pypi.org/project/agentql/) library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install agentql"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Install the Playwright dependency required by AgentQL."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!playwright install chromium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can [store](https://medium.com/@parthdasawant/how-to-use-secrets-in-google-colab-450c38e3ec75) keys in Google Colab's secrets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from google.colab import userdata\n",
    "\n",
    "os.environ[\"AGENTQL_API_KEY\"]=userdata.get('AGENTQL_API_KEY')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run AgentQL script. Please note that an online environment like Google Colab supports **asynchronous version** of AgentQL."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import agentql\n",
    "\n",
    "from playwright.async_api import async_playwright\n",
    "\n",
    "# Set the URL to the desired website\n",
    "URL = \"https://scrapeme.live/shop\"\n",
    "\n",
    "SEARCH_QUERY = \"\"\"\n",
    "{\n",
    "    search_products_box\n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "STOCK_NUMBER_QUERY = \"\"\"\n",
    "{\n",
    "    number_in_stock\n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "async with async_playwright() as playwright, await playwright.chromium.launch(headless=True) as browser:\n",
    "\n",
    "        page = await agentql.wrap_async(await browser.new_page())\n",
    "\n",
    "        await page.goto(URL)\n",
    "\n",
    "        # Use query_elements() method to locate the search product box from the page\n",
    "        response = await page.query_elements(SEARCH_QUERY)\n",
    "\n",
    "        # Use Playwright's API to fill the search box and press Enter\n",
    "        await response.search_products_box.type(\"Charmander\")\n",
    "        await page.keyboard.press(\"Enter\")\n",
    "\n",
    "        # Use query_data() method to fetch the stock number from the page\n",
    "        response = await page.query_data(STOCK_NUMBER_QUERY)\n",
    "\n",
    "        print(response)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
